﻿#pragma once

#include "DB/sqlite3.h"
#include <string>
#include <memory>
#include <vector>

#pragma  comment(lib,"DB/sqlite3.lib")

struct SpellConfig;
struct Property;

class DbTool
{
public:
	static DbTool& inst();

	~DbTool();

	bool ConnectDB();
	bool CloseDB();

	bool InsertEmptySpellConfig(SpellConfig& sc);
	using SpellConfigPtr = std::shared_ptr<SpellConfig>;
	using VecSpellConfig = std::vector<SpellConfigPtr>;
	const VecSpellConfig& GetSpellConfig();

	using PropertyConfigPtr = std::shared_ptr<Property>;
	using VecPropertyConfig = std::vector<PropertyConfigPtr>;

	bool LoadAllSpellDef();
	bool UpdateProperty(Property& prop);
	const VecPropertyConfig& GetPropertyConfig();
	bool LoadPropertyDef();
private:
	sqlite3 *db_ = NULL;
	char **results_;
	char *errmsg_ = NULL;
	int i, nrows_, ncols_, tr_;
	int rc_;

	VecSpellConfig vec_spell_config_;
	VecPropertyConfig vec_property_config;
	DbTool();
	void CheckTable();
	bool IsTableExist(std::string table_name);
	void CreateTable_SpellDef();
	void WriteCurrentSpellConfigToDB();
	void InsertSpellConfigToTable(SpellConfig& sc, sqlite3_stmt* stmt);
};

